package sudoku.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import language.Messages;
import utils.Constants;

/**
 * @author interfaz de usuario con menu al costado del tablero, incluye iconos
 * 
 */
public class SecondGui extends sudoku.view.AbstractView
{

	private JFrame			frame_				= new JFrame();
	private final Color	CELESTE				= new Color(155, 170, 190);
	private final Color	CELESTE_BOTON	= new Color(0, 0, 255);

	private JPanel			contentPane_;
	JTextField					matrix[][]		= new JTextField[9][9];
	private final int		ALTO					= 1;
	private JLabel			lblTime_;

	/**
	 * Create the frame.
	 */
	@SuppressWarnings("static-access")
	public SecondGui() {
		frame_.setTitle(Messages.getString("Gui.0")); //$NON-NLS-1$
		frame_.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame_.setBounds(100, 100, 450, 300);
		frame_.setSize(900, 600);
		frame_.setLocationRelativeTo(null);
		frame_.setVisible(true);

		controller_.setView((IView) this);

		controllerMatrix_.setView((IView) this);

		contentPane_ = new JPanel();
		contentPane_.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane_.setLayout(new BorderLayout(0, 0));
		frame_.setContentPane(contentPane_);

		// matriz
		JPanel panel = new JPanel();
		contentPane_.add(panel, BorderLayout.CENTER);
		GridLayout grilla = new GridLayout(9, 9);
		panel.setLayout(grilla);
		for (int i = 0; i < matrix.length; ++i) {
			for (int j = 0; j < matrix.length; ++j) {
				matrix[i][j] = new JTextField();
				listenerMatrix_.associate(matrix[i][j], controllerMatrix_, i, j);
				Font f = new Font("TimesRoman", Font.BOLD, 36); //$NON-NLS-1$
				matrix[i][j].setHorizontalAlignment((int) frame_.CENTER_ALIGNMENT);
				matrix[i][j].setFont(f);
				matrix[i][j].setCaretColor(Color.WHITE);// color del cursor
				matrix[i][j].setForeground(Color.WHITE);// color del string de entrada
				matrix[i][j].setEditable(true);
				matrix[i][j].setEnabled(false);
				panel.add(matrix[i][j]);
			}
		}
		paintMatrix();

		// barra izquierda
		JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
		contentPane_.add(toolBar, BorderLayout.WEST);

		JButton btnNuevoJuego = new JButton(
				Messages.getString("Gui.nuevo_juego"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/newGame.png")); //$NON-NLS-1$
		btnNuevoJuego.setForeground(CELESTE_BOTON);
		btnNuevoJuego.setBorder(null);
		toolBar.add(btnNuevoJuego);
		buttonListener_.associate(btnNuevoJuego, controller_);

		toolBar.addSeparator();

		JButton btnDetener = new JButton(
				Messages.getString("Gui.detener"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/detener.png")); //$NON-NLS-1$
		btnDetener.setForeground(CELESTE_BOTON);
		btnDetener.setBorder(null);
		toolBar.add(btnDetener);
		buttonListener_.associate(btnDetener, controller_);

		toolBar.addSeparator();

		JButton btnResolver = new JButton(
				Messages.getString("Gui.resolver"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/resolver.png")); //$NON-NLS-1$
		btnResolver.setBorder(null);
		btnResolver.setForeground(CELESTE_BOTON);
		toolBar.add(btnResolver);
		buttonListener_.associate(btnResolver, controller_);

		toolBar.addSeparator();

		JButton btnBorrar = new JButton(
				Messages.getString("Gui.borrar"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/borrar.png")); //$NON-NLS-1$
		btnBorrar.setBorder(null);
		btnBorrar.setForeground(CELESTE_BOTON);
		toolBar.add(btnBorrar);
		buttonListener_.associate(btnBorrar, controller_);

		JButton btnPista = new JButton(
				Messages.getString("Gui.pista"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/pista.png")); //$NON-NLS-1$
		toolBar.add(btnPista);
		btnPista.setBorder(null);
		btnPista.setForeground(CELESTE_BOTON);
		buttonListener_.associate(btnPista, controller_);

		toolBar.addSeparator();

		JButton btnAyuda = new JButton(
				Messages.getString("Gui.acerca_de"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/AcercaDe.png")); //$NON-NLS-1$
		btnAyuda.setBorder(null);
		btnAyuda.setForeground(CELESTE_BOTON);
		toolBar.add(btnAyuda);
		buttonListener_.associate(btnAyuda, controller_);

		toolBar.addSeparator();
		// fin botonera izquierda

		// inicio botonera derecha
		JToolBar toolBar2 = new JToolBar(JToolBar.VERTICAL);
		contentPane_.add(toolBar2, BorderLayout.EAST);

		JButton btnCambVista = new JButton(
				Messages.getString("Gui.cambiar_vista"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/swapGui.png")); //$NON-NLS-1$
		btnCambVista.setBorder(null);
		btnCambVista.setForeground(CELESTE_BOTON);
		toolBar2.add(btnCambVista);
		buttonListener_.associate(btnCambVista, controller_);

		toolBar2.addSeparator();

		JButton btnPreferencias = new JButton(
				Messages.getString("Gui.preferencias"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/preferencias.png")); //$NON-NLS-1$
		btnPreferencias.setBorder(null);
		btnPreferencias.setForeground(CELESTE_BOTON);
		toolBar2.add(btnPreferencias);
		buttonListener_.associate(btnPreferencias, controller_);

		toolBar2.addSeparator();

		JButton btnTopTen = new JButton(
				Messages.getString("Gui.topten"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/topten.png")); //$NON-NLS-1$
		toolBar2.add(btnTopTen);
		btnTopTen.setBorder(null);
		btnTopTen.setForeground(CELESTE_BOTON);
		buttonListener_.associate(btnTopTen, controller_);

		JButton btnSalir = new JButton(
				Messages.getString("Gui.salir"), new ImageIcon( //$NON-NLS-1$
						"src/sudoku/view/imagenes/salir.png")); //$NON-NLS-1$
		btnSalir.setBorder(null);
		btnSalir.setForeground(CELESTE_BOTON);
		toolBar2.add(btnSalir);
		buttonListener_.associate(btnSalir, controller_);

		lblTime_ = new JLabel();
		toolBar2.add(lblTime_);
		// fin botonera
	}

	/**
	 * pinta la matriz
	 */
	private void paintMatrix()
	{
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix.length; j++) {
				matrix[i][j]
						.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
				matrix[i][j].setBackground(CELESTE);
			}
		}
		paintCells(3, 0, this.ALTO);
		paintCells(0, 3, this.ALTO);
		paintCells(6, 3, this.ALTO);
		paintCells(3, 6, this.ALTO);
	}

	/**
	 * @param x
	 * @param y
	 * @param altura
	 * 							Pinta las celdas
	 */
	private void paintCells(int x, int y, int altura)
	{
		for (int i = x; i < x + 3; ++i) {
			for (int j = y; j < y + 3; ++j) {
				if (altura == this.ALTO) {
					matrix[i][j].setBorder(BorderFactory.createLineBorder(Color.WHITE));
					matrix[i][j].setBackground(Color.LIGHT_GRAY);
				} else
					matrix[i][j].setBorder(BorderFactory.createEtchedBorder());
			}
		}
	}

	/**
	 * @param m Matriz con numeros.
	 *          Dibuja la matriz del sudoku. Si se presiono
	 *          detener se le pasa null y se resetea la matriz
	 */
	public void drawingBoard(int[][] m)
	{
		for (int i = 0; i < 9; ++i) {
			for (int j = 0; j < 9; ++j) {
				if (m == null || m[i][j] == 0) {
					matrix[i][j].setText(""); //$NON-NLS-1$
					matrix[i][j].setEnabled(true);
				} else {
					if (m[i][j] != 0) {
						matrix[i][j].setText(m[i][j] + "");// fill sudoku //$NON-NLS-1$
						matrix[i][j].setEnabled(false);
						matrix[i][j].setDisabledTextColor(Color.BLACK);
						// para cuando se preciona detener
					}
				}
			}
		}
	}

	@Override
	public void blockBoard()
	{
		for (int i = 0; i < matrix.length; ++i)
			for (int j = 0; j < matrix.length; ++j) {
				matrix[i][j].setText("");
				matrix[i][j].setEnabled(false);
				matrix[i][j].setEditable(false);
			}
	}
	
	// ingresa un numero en la matriz de jTextFields
	public void setNumberInSudoku(int posI, int posJ, int number, int constant)
	{
		if (constant == Constants.DIBUJARPISTA) {
			matrix[posI][posJ].setText(number + ""); //$NON-NLS-1$
			matrix[posI][posJ].setEnabled(false);
			matrix[posI][posJ].setDisabledTextColor(Color.BLACK);
		} else if (constant == Constants.TECLA_INGRESADA)
			if (number == 0)
				matrix[posI][posJ].setText(""); //$NON-NLS-1$
			else
				matrix[posI][posJ].setText(number + ""); //$NON-NLS-1$
	}

	@Override
	public void dispose()
	{
		frame_.dispose();
	}

//	actualiza el tiempo en la vista
	@Override
	public void setTime(String time)
	{
		lblTime_.setText(time);
	}

}
